﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using VO;

namespace ManagementBooks
{
    public partial class KhachHang : Form
    {
        public KhachHang()
        {
            InitializeComponent();
        }
        KhachHang_BUS _KhachHangBUS = new KhachHang_BUS();
        private void loadallkhachhang()
        {
            loadcbb();
            DataTable dt = new DataTable();
            dt = _KhachHangBUS.allKhachHang();
            GrvDanhSachKH.Rows.Clear();
            if(dt.Rows.Count>0)
            {
                foreach(DataRow row in dt.Rows)
                {
                    DataGridViewRow rows = new DataGridViewRow();
                    rows.CreateCells(GrvDanhSachKH);
                    rows.Cells[1].Value = row["MaKhachHang"].ToString();
                    rows.Cells[2].Value = row["HoTen"].ToString();
                    rows.Cells[3].Value = row["GhiChu"].ToString();
                    rows.Cells[4].Value = row["GioiTinh"].ToString() == "1" ? "Nam" : "Nữ";
                    rows.Cells[5].Value = row["DiaChi"].ToString();
                    rows.Cells[6].Value = row["DienThoai"].ToString();
                    GrvDanhSachKH.Rows.Add(rows);
                }
            }        
            lbTong.ForeColor = Color.Blue;
            lbTong.Text ="Có tổng số : " +_KhachHangBUS.allKhachHang().Rows.Count.ToString()+" khách hàng !";
        }

        private void GrvDanhSachKH_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            for (int i = 0; i < GrvDanhSachKH.RowCount; i++)
                GrvDanhSachKH.Rows[i].Cells[0].Value = i + 1;
        }

        private void KhachHang_Load(object sender, EventArgs e)
        {
            loadallkhachhang();
            SetAdd(false);
        }
        int _option = 0;
        void SetAdd(bool edit)
        {
            txtDiaChi.Clear();
            txtDiaChiTK.Clear();
            txtDienThoai.Clear();
            txtDT.Clear();
            txtGhiChu.Clear();
            txtHoTenKH.Clear();
            txtMaKH.Clear();
            txtTenKH.Clear();
            txtTenKH.Enabled = edit;
            txtDiaChi.Enabled = edit;
            btnXoa.Visible = !edit;
            btnSua.Visible= !edit;
            btnThem.Visible=!edit;
            txtHoTenKH.ReadOnly = edit;
            txtDiaChiTK.ReadOnly = edit;
            txtDienThoai.ReadOnly = edit;
            txtDT.Enabled = edit;
            optNam.Enabled = edit;
            optNu.Enabled = edit;
            txtGhiChu.Enabled = edit;
            btnLuu.Visible = edit;
            btnThoat.Visible = edit;
            GrvDanhSachKH.Enabled = !edit;
            _option = edit ? 1 : 0;
        }
        void SetEdit()
        {
            txtTenKH.Enabled = true;
            txtDiaChi.Enabled = true;
            txtDT.Enabled = true;
            optNam.Enabled = true;
            optNu.Enabled = true;
            txtGhiChu.Enabled = true;
            btnSua.Visible = false;
            btnXoa.Visible = false;
            btnThem.Visible = false;
            btnLuu.Visible = true;
            btnThoat.Visible = true;
            GrvDanhSachKH.Enabled = false;
            grvTK.Enabled = true;
            _option = 2;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            SetAdd(true);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            SetAdd(false);
        }
        bool ketqua = false;
        private void btnLuu_Click(object sender, EventArgs e)
        {
            KhachHang_OBJ obj = new KhachHang_OBJ();
            if(txtMaKH.Text!="")
            {
                obj.MaKhachHang = Convert.ToInt32(txtMaKH.Text);
            }
            else
            {
                obj.MaKhachHang = 0;
            }
            obj.HoTen = txtTenKH.Text;
            obj.GioiTinh = optNam.Checked ? "1" : "0";
            obj.DiaChi = txtDiaChi.Text;
            obj.GhiChu = txtGhiChu.Text;
            obj.DienThoai = txtDT.Text;
            obj.Option = Convert.ToString(_option);
            ketqua = BUS.KhachHang_BUS.Customer_Insert_Up(obj);
            if (!ketqua)
            {
                MessageBox.Show("Thêm sửa khách hàng thất bại !");
            }
            else
            {
                MessageBox.Show("Thêm sửa khách hàng thành công!");
                loadallkhachhang();
                SetAdd(false);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaKH.Text == "")
            {
                return;
            }
            else
            {
                if (DialogResult.Yes == MessageBox.Show("Bạn có chắc muốn xoá khách hàng", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    ketqua = _KhachHangBUS.Del(Convert.ToInt32(txtMaKH.Text));
                    if (!ketqua)
                    {
                        MessageBox.Show("Xoá khách hàng thất bại!");
                    }
                    else
                    {
                        MessageBox.Show("Xoá khách hàng thành công!");
                        loadallkhachhang();
                    }
                }
            }         
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaKH.Text == "")
                return;
            SetEdit();
        }

        private void GrvDanhSachKH_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong;
            dong = e.RowIndex;
            this.txtMaKH.Text = GrvDanhSachKH.Rows[dong].Cells["MaKhachHang"].Value.ToString();
            this.txtTenKH.Text = GrvDanhSachKH.Rows[dong].Cells["HoTen"].Value.ToString();
            this.txtDiaChi.Text = GrvDanhSachKH.Rows[dong].Cells["DiaChi"].Value.ToString();
            this.txtDT.Text = GrvDanhSachKH.Rows[dong].Cells["DienThoai"].Value.ToString();
            this.txtGhiChu.Text = GrvDanhSachKH.Rows[dong].Cells["GhiChu"].Value.ToString();
            if (GrvDanhSachKH.Rows[dong].Cells["GioiTinh"].Value.ToString() == "Nam")
            {
                optNam.Checked = true;
            }
            else
            {
                optNu.Checked = true;
            }
        }
        private void loadcbb()
        {
            cbbDc.DataSource = _KhachHangBUS.allKhachHang();
            cbbDc.DisplayMember = "DiaChi";
            cbbDc.ValueMember = "DiaChi";
            this.cbbDc.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.cbbDc.AutoCompleteSource = AutoCompleteSource.ListItems;
            cbbDT.DataSource = _KhachHangBUS.allKhachHang();
            cbbDT.DisplayMember = "DienThoai";
            cbbDT.ValueMember = "DienThoai";
            this.cbbDT.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.cbbDc.AutoCompleteSource = AutoCompleteSource.ListItems;
        }

        private void btnTKTC_Click(object sender, EventArgs e)
        {
            lbStatus.Text = "";
            if(txtHoTenKH.Text==""&&txtDiaChiTK.Text==""&&txtDienThoai.Text=="")
            {
                MessageBox.Show("Bạn chưa nhập từ khoá tìm kiếm");
            }
            else if (txtDiaChiTK.Text == "" && txtDienThoai.Text == "")
            {
                loadcbb();
                DataTable dt = new DataTable();
                dt = _KhachHangBUS.Search_KH("1",txtHoTenKH.Text,"","");
                GrvDanhSachKH.Rows.Clear();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        DataGridViewRow rows = new DataGridViewRow();
                        rows.CreateCells(GrvDanhSachKH);
                        rows.Cells[1].Value = row["MaKhachHang"].ToString();
                        rows.Cells[2].Value = row["HoTen"].ToString();
                        rows.Cells[3].Value = row["GhiChu"].ToString();
                        rows.Cells[4].Value = row["GioiTinh"].ToString() == "1" ? "Nam" : "Nữ";
                        rows.Cells[5].Value = row["DiaChi"].ToString();
                        rows.Cells[6].Value = row["DienThoai"].ToString();
                        GrvDanhSachKH.Rows.Add(rows);
                    }
                }
                else
                {
                    lbStatus.ForeColor = Color.Red;
                    lbStatus.Text = "Không tìm thấy khách hàng nào !";
                }
                lbTong.ForeColor = Color.Blue;
                lbTong.Text = "Có tổng số : " + _KhachHangBUS.allKhachHang().Rows.Count.ToString() + " khách hàng !";
            }
            else if(txtHoTenKH.Text==""&&txtDienThoai.Text=="")
            {
                loadcbb();
                DataTable dt = new DataTable();
                dt = _KhachHangBUS.Search_KH("2","",txtDiaChiTK.Text, "");
                GrvDanhSachKH.Rows.Clear();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        DataGridViewRow rows = new DataGridViewRow();
                        rows.CreateCells(GrvDanhSachKH);
                        rows.Cells[1].Value = row["MaKhachHang"].ToString();
                        rows.Cells[2].Value = row["HoTen"].ToString();
                        rows.Cells[3].Value = row["GhiChu"].ToString();
                        rows.Cells[4].Value = row["GioiTinh"].ToString() == "1" ? "Nam" : "Nữ";
                        rows.Cells[5].Value = row["DiaChi"].ToString();
                        rows.Cells[6].Value = row["DienThoai"].ToString();
                        GrvDanhSachKH.Rows.Add(rows);
                    }
                }
                else
                {
                    lbStatus.ForeColor = Color.Red;
                    lbStatus.Text = "Không tìm thấy khách hàng nào !";
                }
                lbTong.ForeColor = Color.Blue;
                lbTong.Text = "Có tổng số : " + _KhachHangBUS.allKhachHang().Rows.Count.ToString() + " khách hàng !";
            }
            else if (txtHoTenKH.Text == "" && txtDiaChiTK.Text == "")
            {
                loadcbb();
                DataTable dt = new DataTable();
                dt = _KhachHangBUS.Search_KH("3", "","",txtDienThoai.Text);
                GrvDanhSachKH.Rows.Clear();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        DataGridViewRow rows = new DataGridViewRow();
                        rows.CreateCells(GrvDanhSachKH);
                        rows.Cells[1].Value = row["MaKhachHang"].ToString();
                        rows.Cells[2].Value = row["HoTen"].ToString();
                        rows.Cells[3].Value = row["GhiChu"].ToString();
                        rows.Cells[4].Value = row["GioiTinh"].ToString() == "1" ? "Nam" : "Nữ";
                        rows.Cells[5].Value = row["DiaChi"].ToString();
                        rows.Cells[6].Value = row["DienThoai"].ToString();
                        GrvDanhSachKH.Rows.Add(rows);
                    }
                }
                else
                {
                    lbStatus.ForeColor = Color.Red;
                    lbStatus.Text = "Không tìm thấy khách hàng nào !";
                }
                lbTong.ForeColor = Color.Blue;
                lbTong.Text = "Có tổng số : " + _KhachHangBUS.allKhachHang().Rows.Count.ToString() + " khách hàng !";
            }
        }

        private void txtHoTenKH_TextChanged(object sender, EventArgs e)
        {
            txtDiaChiTK.Clear();
            txtDienThoai.Clear();
        }

        private void txtDiaChiTK_TextChanged(object sender, EventArgs e)
        {
            txtDienThoai.Clear();
            txtHoTenKH.Clear();
        }

        private void txtDienThoai_TextChanged(object sender, EventArgs e)
        {
            txtHoTenKH.Clear();
            txtDiaChiTK.Clear();
        }
    }
}
